﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument
{
    /// <summary>
    /// Project 的摘要说明。
    /// </summary>
    public partial class ProjectPage : Page
    {
        public static string Action = "";
        public static bool bSubscription;
        public static string Username;
        public int classID;


        private void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["classID"] != null)
            {
                classID = Int32.Parse(Request.QueryString["classID"]);
            }
            Action = (Request.QueryString["Action"] != null) ? Request.QueryString["Action"] : "";
            if (Action == "5") SubscribeProject();
            Username = SiteContext.Current.UserName;
            if (Request.QueryString["Subscription"] == null)
            {
                bSubscription = false;
            }
            else
            {
                bSubscription = Int32.Parse(Request.QueryString["Subscription"]) > 0 ? true : false;
            }

            if (!Page.IsPostBack)
            {
                PopulateData();
            }
        }

        public string GetRealName(string Username)
        {
            if (Username != "")
                return Components.Staff.GetRealNameByUsername(Username);
            else
                return "";
        }

        #region 显示数据

        /// <summary>
        /// 显示数据
        /// </summary>
        private void PopulateData()
        {
            #region 初始化数据

            var dataTable = new DataTable();
            SqlDataReader dataReader = null;
            var pjt = new ProjectClass();
            var mail = new MailClass();
            var doc = new DocumentClass();

            #endregion

            #region 获取项目信息

            dataReader = pjt.GetClassInfo(classID);
            try
            {
                if (dataReader.Read())
                {
                    lblClassName.Text = dataReader[0].ToString();
                    //			this.lblParentClassName.Text = dataReader[2].ToString();
                    //			this.lblBuildDate.Text	     = dataReader[3].ToString();
                    //			this.lblEndDate.Text		 = dataReader[4].ToString();
                    //			this.lblFinishedScale.Text   = dataReader[8].ToString();
                    //			this.lblDescription.Text	 = dataReader[7].ToString();
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
            dataReader = null;

            #endregion

            #region 获取项目成员信息

            var staffList = Staff.GetStaffInTeam(classID);
            int i = 0;
            foreach (var staff in staffList)
            {
                if (i < 3)
                    lblMember.Text += "<a href='Mail/Compose.aspx?Action=3"
                                      + "&ClassID=" + classID
                                      + "&Username=" + HttpUtility.HtmlAttributeEncode(staff.Name)
                                      + "&Name=" + HttpUtility.HtmlAttributeEncode(staff.RealName)
                                      + "'>"
                                      + HttpUtility.HtmlEncode(staff.RealName)
                                      + "</a> &nbsp;&nbsp;";
                i++;
            }
            if (i >= 3)
            {
                lblMember.Text += "..";
            }
            if (lblMember.Text == "")
            {
                lblMember.Text = "";
            }

            #endregion

            #region 根据权限显示相关功能标题

            // 设置缺省值
            lblManageProject.Visible = AssignRights.IsAccessAllowed(Username, classID, PermissionType.ManageNodes);
            imgManageProject.Visible = lblManageProject.Visible;
            lblManageDirectory.Visible = AssignRights.IsAccessAllowed(Username, classID, PermissionType.ManageNodes);
            imgManageDirectory.Visible = lblManageDirectory.Visible;
            lblComposeMail.Visible = AssignRights.IsAccessAllowed(Username, classID, PermissionType.DeliveryDocument);
            imgComposeMail.Visible = lblComposeMail.Visible;
            lblDeliveryDoc.Visible = AssignRights.IsAccessAllowed(Username, classID, PermissionType.DeliveryDocument);
            imgDeliveryDoc.Visible = lblDeliveryDoc.Visible;
            lblShowMember.Visible = AssignRights.IsAccessAllowed(Username, classID, PermissionType.ManageTeam);
            imgShowMember.Visible = lblShowMember.Visible;
            lblManagePermission.Visible = AssignRights.IsAccessAllowed(Username, classID, PermissionType.ManagePermission);
            imgManagePermission.Visible = lblManagePermission.Visible;
            lblRemove.Visible = AssignRights.IsAccessAllowed(Username, classID, PermissionType.MoveProject);
            imgRemove.Visible = lblRemove.Visible;

            line1.Visible = AssignRights.IsAccessAllowed(Username, classID, PermissionType.ViewDocument);
            IMG1.Visible = line1.Visible;
            dgDocList.Visible = line1.Visible;
            imgSubscribe.Visible = line1.Visible;
            lblSubscribe.Visible = line1.Visible;
            line2.Visible = AssignRights.IsAccessAllowed(Username, classID, PermissionType.ApproveDocument);
            IMG3.Visible = line2.Visible;
            dgAppDocList.Visible = line2.Visible;

            #endregion

            #region 根据权限取出邮件数据

            /// <summary>
            /// 将某用户的取出邮件数据
            /// </summary>

            dataTable = mail.GetClassMails(classID, SiteContext.Current.UserName);
            dgMailList.DataSource = dataTable.DefaultView;
            dgMailList.DataBind();

            #endregion

            #region 根据权限取出文档数据

            /// <summary>
            /// 将某用户的文档数据取出
            /// </summary>
            if (AssignRights.IsAccessAllowed(Username, classID, PermissionType.ViewDocument) || bSubscription)
            {
                dataTable = doc.GetClassDocs(classID);
                dgDocList.DataSource = dataTable.DefaultView;
                dgDocList.DataBind();
                dataTable = null;
            }
            else
            {
                //				IMG1.Visible	  = false;
                //				IMG4.Visible      = false;
                dgDocList.Visible = false;
            }

            #endregion

            #region 根据权限取出某项目的审批文档数据

            /// <summary>
            /// 将某项目的审批文档数据取出
            /// </summary>
            if (AssignRights.IsAccessAllowed(Username, classID, PermissionType.ApproveDocument))
            {
                dataTable = doc.GetApproveClassDocs(classID);
                dgAppDocList.DataSource = dataTable.DefaultView;
                dgAppDocList.DataBind();
                dataTable = null;
                doc = null;
            }
            else
            {
                IMG3.Visible = false;
                dgAppDocList.Visible = false;
                lblApp.Visible = false;
            }

            #endregion
        }

        #endregion

        #region 订阅项目

        /// <summary>
        /// 订阅项目
        /// </summary>
        private void SubscribeProject()
        {
            var prj = new ProjectClass();
            try
            {
                prj.Subscribe(Username, classID);
                prj = null;
                Response.Write("<script language=javascript>alert('订阅成功!');</script>");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
        }

        #endregion

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}